package com.sny.radish.upgradeclientdemo;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;
import com.sny.upgrade.AppUpgradeImple;
import com.sny.upgrade.firebase.config.IFireBaseConfigObserver;

import java.util.ArrayList;

/**
 * Created by tyler.tang on 2017/6/7.
 * <p>
 * fireBase config 和整个程序的接口处
 */

public class LabFireBaseRemoteConfig {


    private Context mContext;
    private Activity mHomeActivity;

    /*firebase remote config */
    private FirebaseRemoteConfig mFirebaseRemoteConfig = null;
    /* firebase 这里又要重新的构建*/
    public ArrayList<IFireBaseConfigObserver> mFireBaseObservers = new ArrayList<>();


    public LabFireBaseRemoteConfig(Context context, Activity homeActivity) {
        this.mContext = context;
        this.mHomeActivity = homeActivity;
    }


    /**
     * 初始化远程配置
     */
    public void initRemoteConfig() {

        mFireBaseObservers.add(new AppUpgradeImple(mContext.getApplicationContext(), mHomeActivity));

        // Get Remote Config instance.
        // [START get_remote_config_instance]
        mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
        // [END get_remote_config_instance]

        // Create a Remote Config Setting to enable developer mode, which you can use to increase
        // the number of fetches available per hour during development. See Best Practices in the
        // README for more information.
        // [START enable_dev_mode]
        FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
                .setDeveloperModeEnabled(BuildConfig.DEBUG)
                .build();
        mFirebaseRemoteConfig.setConfigSettings(configSettings);
        // [END enable_dev_mode]

        // Set default Remote Config parameter values. An app uses the in-app default values, and
        // when you need to adjust those defaults, you set an updated value for only the values you
        // want to change in the Firebase console. See Best Practices in the README for more
        // information.
        // [START set_default_values]
        mFirebaseRemoteConfig.setDefaults(R.xml.remote_default_value);

        fetchValue();
    }


    private void fetchValue() {

        long cacheExpiration = 3600; // 1 hour in seconds.
        // If your app is using developer mode, cacheExpiration is set to 0, so each fetch will
        // retrieve values from the service.
        if (mFirebaseRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) {
            cacheExpiration = 0;
        }

        // [START fetch_config_with_callback]
        // cacheExpirationSeconds is set to cacheExpiration here, indicating the next fetch request
        // will use fetch data from the Remote Config service, rather than cached parameter values,
        // if cached parameter values are more than cacheExpiration seconds old.
        // See Best Practices in the README for more information.
        Task task = mFirebaseRemoteConfig.fetch(cacheExpiration)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        android.util.Log.i("tyler.tang", "onComplete");

                        if (task.isSuccessful()) {
                            mFirebaseRemoteConfig.activateFetched();

                            //处理配置变化信息
                            for (IFireBaseConfigObserver observer : mFireBaseObservers) {
                                observer.onConfigFetchComplete(task);
                            }
                        }
                    }
                });

        task.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                android.util.Log.i("tyler.tang", "onFailure");
            }
        });

        task.addOnSuccessListener(new OnSuccessListener() {
            @Override
            public void onSuccess(Object o) {
                android.util.Log.i("tyler.tang", "onSuccess");
            }
        });
    }


}
